/*
 * test_net_s.cpp
 *
 *  Created on: May 19, 2014
 *      Author: killerg
 */

#include "share/net_s.h"
#include "share/cs_msg.pb.h"
#include "share/clock.h"
#include <assert.h>
#include <vector>
using namespace cs;
using namespace std;

bool test_net_s()
{
	my_clock_t clock;
	clock.init(50);
	net_s nets;
	nets.init(10000, NULL, 1000, true, 1000);

	std::vector<tran_s*> transes;

	bool ok = nets.start_accept(
			"0.0.0.0",
			9876,
			[&transes](tran_s* tran){
				transes.push_back(tran);
			},
			[&transes](tran_s* tran){
				int index = -1;
				for (int i=0; index<0 && i<transes.size(); ++i) {
					if (tran == transes[i])
						index = i;
				}

				swap(transes[index], transes[transes.size()-1]);
				transes.pop_back();
			}
			);
	assert(ok);

	while (true) {
		clock.begin_update();
		for (auto itr : transes) {
			auto msg = itr->recv<cs::OEcho>();
			if (msg)
				assert(itr->send(msg));
		}
		nets.update();
		clock.end_update();
		clock.wait_frame();
		if (clock.sec_jump) {
			printf("%d\n", transes.size());
		}
	}

	nets.stop_accept();

	nets.destroy();
	clock.destroy();
	return true;
}
